<?php

    /**
    * This is the model class for table "manager".
    *
    * The followings are the available columns in table 'manager':
    * @property string $id
    * @property string $phone
    * @property string $password
    * @property string $name
    * @property string $address
    * @property string $status
    *
    * The followings are the available model relations:
    * @property Bill[] $bills
    * @property Branch[] $branches
    * @property ManagerUpdateBill[] $managerUpdateBills
    * @property Post[] $posts
    */
    class Manager extends Model
    {

        /**
        * Returns the static model of the specified AR class.
        * @param string $className active record class name.
        * @return Manager the static model class
        */
        public static function model($className=__CLASS__)
        {
            return parent::model($className);
        }

        /**
        * @return string the associated database table name
        */
        public function tableName()
        {
            return 'manager';
        }

        /**
        * @return array validation rules for model attributes.
        */
        public function rules()
        {
            // NOTE: you should only define rules for those attributes that
            // will receive user inputs.
            return array(
                array('phone, password, name, address', 'required'),
                array('status', 'required', 'message' => '{attribute} phải được chọn'),
                array('phone', 'length', 'max'=>11),
                array('password', 'length', 'max'=>40),
                array('name', 'length', 'max'=>50),
                array('address', 'length', 'max'=>255),

                array('phone', 'match', 'pattern' => '/^0[91]\d{8,9}$/', 'message' => '{attribute} phải đúng định dạng'), 

                array('phone', 'unique', 'className' => 'Manager', 'attributeName' => 'phone', 'message' => '{attribute} đã được sử dụng với tài khoản khác'),

                // The following rule is used by search().
                // Please remove those attributes that should not be searched.
                array('id, phone, password, name, address, status', 'safe', 'on'=>'search'),
            );
        }

        /**
        * @return array relational rules.
        */
        public function relations()
        {
            // NOTE: you may need to adjust the relation name and the related
            // class name for the relations automatically generated below.
            return array(
                'posts' => array(self::HAS_MANY, 'Post', 'manager_id'),       
                'bills' => array(self::HAS_MANY, 'Bill', 'manager_id'),
                'managerUpdateBills' => array(self::HAS_MANY, 'ManagerUpdateBill', 'manager_id'),
                'branches' => array(self::MANY_MANY, 'Branch', 'manager_has_branch(manager_id, branch_id)'),
            );
        }

        /**
        * @return array customized attribute labels (name=>label)
        */
        public function attributeLabels()
        {
            return array(
                'id' => 'ID',
                'phone' => 'Điện thoại di động',
                'password' => 'Mật khẩu',
                'name' => 'Tên đầy dủ',
                'address' => 'Địa chỉ',
                'status' => 'Cấp độ',
            );
        }



        /**
        * Retrieves a list of models based on the current search/filter conditions.
        * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
        */
        public function search()
        {
            // Warning: Please modify the following code to remove attributes that
            // should not be searched.

            $criteria=new CDbCriteria;

            $criteria->compare('id',$this->id,true);
            $criteria->compare('phone',$this->phone,true);
            $criteria->compare('password',$this->password,true);
            $criteria->compare('name',$this->name,true);
            $criteria->compare('address',$this->address,true);
            $criteria->compare('status',$this->status,true);

            return new CActiveDataProvider($this, array(
                'criteria'=>$criteria,
            ));
        }

        public function getIsStaff(){
            return in_array($this->status, array('STAFF', 'MANAGER', 'ADMIN'));
        }
        public function getIsManager(){
            return in_array($this->status, array('MANAGER', 'ADMIN'));
        }
        public function getIsAdmin(){
            return in_array($this->status, array('ADMIN'));
        }
        public function getIsManagerOnly(){
            return in_array($this->status, array('MANAGER'));
        }
        public function getIsStaffOnly(){
            return in_array($this->status, array('STAFF'));
        }
        public function getIsDisable(){
            return in_array($this->status, array('DISABLE'));
        }


        public function getHashPassword($password){
            Yii::import('ext.PasswordHash');
            $hasher = new PasswordHash(8, FALSE);
            $hash = $hasher->HashPassword($password);
            return $hash;
        }

        public function checkPassword($password, $hashPassword){
            Yii::import('ext.PasswordHash');
            $hasher = new PasswordHash(8, FALSE);
            $check = $hasher->CheckPassword($password, $hashPassword);
            return $check;
        }


        public function getStatusData() {
            return array(
                'ADMIN' => 'Quản lý cao cấp',
                'MANAGER' => 'Quản lý',
                'STAFF' => 'Nhân viên',
                'DISABLE' => 'Khóa',
            );
        }
        public function getStatusLabel() {
            return $this->statusData[$this->status];
        }


        public function getStatusDataByLevel() {
            $data = array();
            if($this->status == 'ADMIN'){
                $data['MANAGER'] = 'Quản lý'; 
            }
            if(in_array($this->status, array('ADMIN', 'MANAGER'))){
                $data['STAFF'] = 'Nhân viên';    
            }
            $data['DISABLE'] = 'Khóa';
            return $data;
        }

        public function getAdminUrl($type = 'view') {
            $params = array();
            if(Yii::app()->controller->manager->id != $this->id)
                $params['id'] = $this->id;

            return Yii::app()->createUrl('/admin/manager/' . $type, $params);
        }

        public function getDataStaff(){
            $managers = $this->findAll('`status` = "STAFF"');
            return CHtml::listData($managers, 'id', 'name');
        }
        

        public function getDataAdmin(){
            $managers = $this->findAll('`status` = "ADMIN"');
            return CHtml::listData($managers, 'id', 'name');
        }
        


}